package com.etrans.web.gps.business.base.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.etrans.web.gps.base.service.BaseService;
import com.etrans.web.gps.business.base.domain.Unit;


@Service
public class UnitService extends BaseService{

	public List<HashMap<String, Object>> queryFullUnitTree(HashMap<String, Object> paramsMap) {
		List list = selectList("unitQuery.queryFullUnitTree", paramsMap);
		return list;
	}
	
	
	public List<HashMap<String, Object>> queryFullUnitTreeByUnitId(HashMap<String, Object> paramsMap) {
		List list = selectList("unitQuery.queryFullUnitTreeByUnitId", paramsMap);
		return list;
	}
	
	public List<HashMap<String, Object>> queryFullUnitTreeByAuth(HashMap<String, Object> paramsMap) {
		List list = selectList("unitQuery.queryFullUnitTreeByAuth", paramsMap);
		return list;
	}
	

	public List queryAllUnitList(Map<String, Object> paramMap) {
		return selectList("unitQuery.queryAllUnitList", paramMap);
	}

	public Integer queryAllUnitListCount(Map<String, Object> paramMap) {
		return (Integer) this.selectOne("unitQuery.queryAllUnitListCount", paramMap);
	}
	//查询当前用户所在平台所有下级企业
	public List<HashMap<String, Object>> querySubFullUnit(Map<String, Object> paramMap) {
		return this.selectList("unitQuery.querySubFullUnit", paramMap);
	}
	
	/**
	 * 根据ID获取单位
	 * 
	 * @param id
	 * @return
	 */
	public Unit queryUnitById(Integer id) {
		Unit unit = (Unit) this.selectOne("unitQuery.queryUnitById", id);
		return unit;
	}

	public List queryAllTradeKinds(String dbKey) {
		return selectList("unitQuery.queryAllTradeKinds", new HashMap());
	}

	public List loadAllOrganzationKinds(String dbKey) {
		return selectList("unitQuery.loadAllOrganzationKinds", new HashMap());
	}

	public List<HashMap<String, Object>> queryUnitTreeExtend(HashMap<String, Object> paramsMap) {
		return selectList("unitQuery.queryAreaTreeExtend", new HashMap());
	}

	public int queryCountByUnitName(HashMap<String, Object> paramsMap) {
		return(Integer) this.selectOne("unitQuery.queryCountByUnitName", paramsMap);
	}
	
	@Transactional(readOnly = false, rollbackFor = { DataAccessException.class, Exception.class })
	public void saveUnit(Unit unit) {
		this.insert("unitMapper.saveUnit", unit);	
		// 拼接8位fullID
		String unitString = unit.getId().toString().trim();
		if (unitString.length() < 8) {
			while (unitString.length() < 8) {
				unitString = "0" + unitString;
			}
		}
		// 获取父节点信息,设置fullID，fullName
		if (unit.getParentId() != null) {
			Unit perentUnit = (Unit) this.selectOne("unitQuery.queryUnitById", unit.getParentId());
			unit.setFullId(perentUnit.getFullId() + "." + unitString);
			unit.setFullName(perentUnit.getFullName() + "." + unit.getName());
		} else {
			unit.setFullId(unitString);
			unit.setFullName(unit.getName());
		}
		// 最后更新FullID,FullName回数据库
		this.update("unitMapper.updateUnit", unit);
	}
	/**
	 * 根据企业查找车辆数
	 * @param UnitID
	 * @return
	 */
	public Integer queryCountByUnitID(Integer WorkUnitID){
		return (Integer) this.selectOne("unitQuery.queryCountByUnitID", WorkUnitID);
	}
	
	@Transactional(readOnly = false, rollbackFor = { DataAccessException.class, Exception.class })
	public void updateUnit(Unit unit) {
		// 获取父节点信息,设置fullID，fullName
		if (unit.getParentId() != null) {
			Unit perentUnit = (Unit) this.selectOne("unitQuery.queryUnitById", unit.getParentId());
			unit.setFullName(perentUnit.getFullName() + "." + unit.getName());
		} else {
			unit.setFullName(unit.getName());
		}
		this.update("unitMapper.updateUnit", unit);
	}

	public Unit queryUnitByName(HashMap<String, Object> param) {
		Unit unit = (Unit) this.selectOne("unitQuery.queryUnitByName", param);
		return unit;
	}
	
	@Transactional(readOnly = false, rollbackFor = { DataAccessException.class, Exception.class })
	public void deleteUnitByID(Integer id) {
		this.update("unitMapper.deleteUnitByID", id);
	}
	
	@Transactional(readOnly = false, rollbackFor = { DataAccessException.class, Exception.class })
	public void changeParentUnit(Unit unit){
		String unitString = unit.getId().toString().trim();
		String unitFullIDString = unit.getFullId().toString().trim();
		if (unitString.length() < 8) {
			while (unitString.length() < 8) {
				unitString = "0" + unitString;
			}
		}
		// 获取父节点信息,设置fullID，fullName
		if (unit.getParentId() != null) {
			Unit perentUnit = (Unit) this.selectOne("unitQuery.queryUnitById",unit.getParentId());
			unit.setFullId(perentUnit.getFullId() + "." + unitString);
			unit.setFullName(perentUnit.getFullName() + "." + unit.getName());
			changeChildrenUnit(unit,unitFullIDString);
		} else {
			unit.setFullId(unitString);
			unit.setFullName(unit.getName());
			changeChildrenUnit(unit,unitFullIDString);
		}
		// 最后更新FullID,FullName回数据库
		this.update("unitMapper.updateUnit", unit);
	}
	
	public void changeChildrenUnit(Unit unit,String unitFullIDString){
		HashMap<String, Object> params = new HashMap<String, Object>();
		params.put("fullID", unitFullIDString);
		List<Unit> unitChildrenList = selectList("unitQuery.queryUnitByFullID", params);
		if(unitChildrenList!=null&&unitChildrenList.size()>0){
			for(int i=0;i<unitChildrenList.size();i++){
				String unitChildID = unitChildrenList.get(i).getId() + "";
				if (unitChildID.length() < 8) {
					while (unitChildID.length() < 8) {
						unitChildID = "0" + unitChildID;
					}
				}
				Unit tmpUnit = (Unit) this.selectOne("unitQuery.queryUnitById",Integer.parseInt(unitChildID));
				tmpUnit.setFullId(unit.getFullId()+"."+unitChildID);
				tmpUnit.setFullName(unit.getFullName()+"."+tmpUnit.getName());
				this.update("unitMapper.updateUnit", tmpUnit);
			}
		}
	}


	public List<Unit> queryUnitExportList(Map<String, Object> paramMap) {
		List list = selectList("unitQuery.queryUnitExportList", paramMap);
		return list;
	}
	
	/**
	 * 用户导入时需要验证企业是否存在
	 * @author liurong
	 * @param param
	 * @return
	 */
	public Unit queryUnitByWorkUnitName(HashMap<String, Object> param){
		Unit unit = (Unit) this.selectOne("unitQuery.queryUnitByWorkUnitName", param);
		return unit;
	}

	/**
	 * 查询是否存在子企业
	 * @param dbKey 连接池标识
	 * @param WorkUnitID 企业ID
	 * @return
	 */
	public Integer queryChildUnitCountByUnitID( Integer WorkUnitID) {
		
		return (Integer) this.selectOne("unitQuery.queryChildUnitCountByUnitID", WorkUnitID);
	}

}
